<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('牛奶报表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li class="select-time">
                                <label>上报时间：</label>
                                <input type="text" class="time-input" id="startTime" placeholder="开始时间" name="params[beginReportDate]"/>
                                <span>-</span>
                                <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="params[endReportDate]"/>
                            </li>
                            <li>
                                <label>班级：</label>
                                <input type="text" name="className"/>
                            </li>
                            <li>
                                <label>院区：</label>
                                <input type="text" name="campus"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-warning" onclick="$.table.exportExcel()" >
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-bordered">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('meal:milk:edit')}]];
        var removeFlag = [[${@permission.hasPermi('meal:milk:remove')}]];
        var phaseDatas = [[${@dict.getType('meal_mikl_phase')}]];
        var prefix = ctx + "meal/milk";

        $(function() {
            var options = {
                url: prefix + "/reportShowList",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/exportReport",
                modalName: "牛奶上报",
                onLoadSuccess:function(data) {
                    // mergeTable(data,'bootstrap-table');
                    mergeCellsT(data, "className", 1, '#bootstrap-table'); //行合并
                    mergeCellsT(data, "campus", 1, '#bootstrap-table'); //行合并
                    mergeCellsT(data, "reportingPerson", 1, '#bootstrap-table'); //行合并
                    // mergeRowspans('#bootstrap-table',0,['className','campus','reportingPerson'],4);
                },
                columns:[
                    [
                        {
                            field: 'className',
                            title: '班级',
                            valign: 'middle',
                            rowspan:2
                        },
                        {
                            field: 'campus',
                            title: '院区',
                            valign: 'middle',
                            rowspan:2
                        },
                        {
                            field: 'phase',
                            title: '阶段',
                            valign: 'middle',
                            rowspan:2
                        },
                        {
                            field: 'water6120',
                            align : 'center',
                            title: '6:00',
                            colspan : 5
                        } ,
                        {
                            field: 'water9120',
                            align : 'center',
                            title: '9:00',
                            colspan : 5
                        },
                        {
                            field: 'water13120',
                            align : 'center',
                            title: '13:00',
                            colspan : 5
                        },
                        {
                            field: 'water16120',
                            align : 'center',
                            title: '16:00',
                            colspan : 5
                        },
                        {
                            field: 'water20120',
                            align : 'center',
                            title: '20:00',
                            colspan : 5
                        },
                        {
                            field: 'water20120',
                            align : 'center',
                            title: '00:00',
                            colspan : 5
                        },

                        {
                            field: 'reportingPerson',
                            title: '填报人',
                            valign: 'middle',
                            rowspan:2
                        },
                        {
                            title: '',
                            align: 'center'
                        }]
                    ,
                    [
                {
                    field: 'water6120',
                    title: '水奶120ml',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'water6200',
                    title: '水奶200ml',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'paste6200',
                    title: '奶糊200ml',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'paste6Te',
                    title: '奶糊特瓶',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'empty6',
                    title: '空瓶',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'water9120',
                    title: '水奶120ml',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'water9200',
                    title: '水奶200ml',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'paste9200',
                    title: '奶糊200ml',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'paste9Te',
                    title: '奶糊特瓶',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'empty9',
                    title: '空瓶',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'water13120',
                    title: '水奶120ml',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'water13200',
                    title: '水奶200ml',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'paste13200',
                    title: '奶糊200ml',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'paste13Te',
                    title: '奶糊特瓶',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'empty13',
                    title: '空瓶',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'water16120',
                    title: '水奶120ml',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'water16200',
                    title: '水奶200ml',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'paste16200',
                    title: '奶糊200ml',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'paste16Te',
                    title: '奶糊特瓶',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'empty16',
                    title: '空瓶',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'water20120',
                    title: '水奶120ml',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'water20200',
                    title: '水奶200ml',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'paste20200',
                    title: '奶糊200ml',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'paste20Te',
                    title: '奶糊特瓶',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                {
                    field: 'empty20',
                    title: '空瓶',
                    formatter: function (value) {
                        return showSeparator(value);
                    }
                },
                        {
                            field: 'water20120',
                            title: '水奶120ml',
                            formatter: function (value) {
                                return showSeparator(value);
                            }
                        },
                        {
                            field: 'water20200',
                            title: '水奶200ml',
                            formatter: function (value) {
                                return showSeparator(value);
                            }
                        },
                        {
                            field: 'paste20200',
                            title: '奶糊200ml',
                            formatter: function (value) {
                                return showSeparator(value);
                            }
                        },
                        {
                            field: 'paste20Te',
                            title: '奶糊特瓶',
                            formatter: function (value) {
                                return showSeparator(value);
                            }
                        },
                        {
                            field: 'empty20',
                            title: '空瓶',
                            formatter: function (value) {
                                return showSeparator(value);
                            }
                        },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-info btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="detail(\'' + row.reportDate  + '\',\'' + row.className  + '\',\'' + row.campus + '\')"><i class="fa fa-list-ul"></i>列表</a> ');
                        return actions.join('');
                    }
                }]
                ]

            };
            $.table.init(options);
        });

        /*报餐明细-详细*/
        function detail(reportDate , className , campus) {
            var url = prefix + '/detail/' + reportDate+"/"+className+"/"+campus;
            $.modal.openTab("牛奶明细", url);
        }

        mergeRowspans = function (targetTable, merIndex,fieldArr,rowSum) {
            for (var i = 0;i < fieldArr.length;i++){
                $(targetTable).bootstrapTable('mergeCells', {index: merIndex, field: fieldArr[i], rowspan: rowSum});
            }
        }

        //
        function  showSeparator(value)
        {
            if (value == undefined || value==0)
            {
                return "-";
            }else {
                return  value;
            }
        }

        var firstSubjectCount="";
        var tegionCount="";

        /**
         * 合并行
         * @param data  原始数据（在服务端完成排序）
         * @param fieldName 合并属性名称数组
         * @param colspan 列数
         * @param target 目标表格对象
         */
        function mergeCellsT(data, fieldName, colspan, target) {
            if (data.length == 0) {
                alert("不能传入空数据");
                return;
            }
            var numArr = [];
            var value = data.rows[0][fieldName];
            console.log(data);
            var num = 0;
            for (var i = 0; i < data.rows.length; i++) {
                if (value != data.rows[i][fieldName]) {
                    numArr.push(num);
                    value = data.rows[i][fieldName];
                    num = 1;
                    continue;
                }if(i==data.rows.length-1 && value == data.rows[i][fieldName]){
                    num++;
                    numArr.push(num);
                    value = data.rows[i][fieldName];
                    continue;
                }
                num++;
            }
            var merIndex = 0;
            for (var i = 0; i < numArr.length; i++) {
                $(target).bootstrapTable('mergeCells', {
                    index: merIndex,
                    field: fieldName,
                    colspan: colspan,
                    rowspan: numArr[i]
                });
                merIndex += numArr[i];
            }
        }

        /**
         * 合并列
         * @param data  原始数据（在服务端完成排序）
         * @param fieldName 合并属性数组
         * @param target    目标表格对象
         */
        function mergeColspan(data, fieldNameArr, target) {
            if (data.length == 0) {
                alert("不能传入空数据");
                return;
            }
            if (fieldNameArr.length == 0) {
                alert("请传入属性值");
                return;
            }
            var num = -1;
            var index = 0;
            for (var i = 0; i < data.rows.length; i++) {
                num++;
                for (var v in fieldNameArr) {
                    index = 1;
                    if (data.rows[i][fieldNameArr[v]] != data.rows[i][fieldNameArr[0]]) {
                        index = 0;
                        break;
                    }
                }

                if (index == 0) {
                    continue;
                }

                $(target).bootstrapTable('mergeCells', {
                    index: num,
                    field: fieldNameArr[0],
                    colspan: fieldNameArr.length,
                    rowspan: 1
                });
            }
        }

        /**
         * 合并单元格
         * @param data 原始数据（在服务端完成排序）
         * @param fieldName 合并属性名称
         * @param colspan  合并列
         * @param target  目标表格对象
         */
        function mergeCells(data, fieldName, colspan, target) {
            //声明一个map计算相同属性值在data对象出现的次数和
            var sortMap = {};
            for (var i = 0; i < data.length; i++) {
                for (var prop in data[i]) {
                    if (prop == fieldName) {
                        var key = data[i][prop]
                        if (sortMap.hasOwnProperty(key)) {
                            sortMap[key] = sortMap[key] * 1 + 1;
                        } else {
                            sortMap[key] = 1;
                        }
                        break;
                    }
                }
            }
            for (var prop in sortMap) {
                console.log(prop, sortMap[prop])
            }

            var index = 0;
            for (var prop in sortMap) {
                var count = sortMap[prop] * 1;
                $(target).bootstrapTable('mergeCells', {
                    index: index,
                    field: fieldName,
                    colspan: colspan,
                    rowspan: count
                });
                index += count;
            }
        }
    </script>
</body>
</html>